<?php
declare(strict_types=1);

namespace app\model;

/**
 * Class Salesman
 *
 * @package app\model
 * @property-read $username
 * @property int $id 自增id
 * @property string $password 密码
 * @property string $true_name 姓名
 * @property string $mobile 手机号码
 * @property int $status 状态: 1启用 2禁用
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property int $is_deleted 是否删除
 * @property-read \app\model\AgentSalesman[] $agent_salesman
 * @property-read string $status_format
 * @property-read mixed $agent_salesman_num
 */
class Salesman extends CommonModel
{

    // 表名
    protected $table = 'salesman';

    // 隐藏字段
    protected $hidden = ['password'];

    // 常量
    const C_STATUS_OPEN = 1;  // 启用
    const C_STATUS_CLOSE = 2;  // 禁用

    /**
     * 状态
     * @var array
     */
    protected static $statusMap = [
        self::C_STATUS_OPEN => '正常',
        self::C_STATUS_CLOSE => '禁用',
    ];

    /**
     * 关联代理商
     * @return \think\model\relation\HasMany
     */
    public function agentSalesman() {
        return $this->hasMany(AgentSalesman::class, 'salesman_id');
    }

    /**
     * 业务员下拉数据 map
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function salesmanMap() {
        $ret = self::where([
            'is_deleted' => DELETE_TYPE_NO,
        ])
            ->field('id as value, true_name as label, status')
            ->order('status asc, id desc')
            ->select()
            ->toArray();
        return $ret;
    }

    /**
     * 状态
     * @param $value
     * @param $model
     * @return string
     */
    public function getStatusFormatAttr($value, $model)
    {
        return self::$statusMap[$model['status']] ?? '未知';
    }

    /**
     * 代理商数量统计
     * @param $value
     * @param $model
     * @return mixed
     */
    public function getAgentSalesmanNumAttr($value, $model) {
        $count = AgentSalesman::agentSalesmanNum($model['id']);
        return $count;
    }

    /**
     * 关联代理商详情
     * @param $id 业务员id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function agentDetail($id) {
        $model = AgentSalesman::where(['salesman_id' => $id, 'is_deleted' => DELETE_TYPE_NO])
            ->with(['agent' => function($query) {
                $query->field('id, username, company_name');
            }])
            ->select()
            ->toArray();

        $data = [];
        if (!$model) {
            return $data;
        }

        foreach ($model as $item) {
            $agentId = $item['agent_id'];
            if (!isset($data[$agentId])) {
                $data[$agentId]['agent_id'] = $agentId;
                $data[$agentId]['username'] = $item['agent']['username'];
                $data[$agentId]['company_name'] = $item['agent']['company_name'];
            }
//            $data[$agentId]['serv_info'][]= SERVICE_TYPE[$item['serv_type']] ?? '';
            $data[$agentId]['serv_info'][]= @PRODUCT_SERV_TYPE[$item['serv_type']]['name']?? '';
        }


        return array_values($data);
    }

}